﻿/*
 * 1. 完成日期<->字符串的基本转换处理 
 * 2. 提供了 无效日期的统一表达 2100-01-01
 * 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace HousingDistribution
{
    class DateProc
    {
        static public DateTime InvalidDate()
        {
            return invalidDate;
        }

        public DateProc()
        {
            dtFI.ShortDatePattern = "YY-MM-DD";
        }

        public DateProc(string pattern)
        {
            dtFI.ShortDatePattern = pattern;
        }

        public DateTime Parse(string timeStr)
        {
            DateTime date;

            try
            {
                if (timeStr.Length != 0)
                {
                    date = DateTime.Parse(timeStr, dtFI);
                    return date;
                }
            }
            catch (Exception)
            {
                return invalidDate;
            }

            return invalidDate;
        }

        public DateTime GetEarlierDate(DateTime date1, DateTime date2)
        {
            if (date1.CompareTo(date2) <= 0)
                return date1;
            else
                return date2;
        }

        public DateTime GetLaterDate(DateTime date1, DateTime date2)
        {
            if (date1.CompareTo(date2) >= 0)
                return date1;
            else
                return date2;
        }

        private static DateTime invalidDate = new DateTime(2100, 1, 1);
        //时间信息格式化
        private DateTimeFormatInfo dtFI = new DateTimeFormatInfo();

    }
}
